-- @owner: zhanghuan
-- @date: 2024-06-19
-- @testpoint: join(子查询)
--step1:创建测试表1; expect:执行成功
drop table if exists t_ustore_listagg_0038_01;
create table t_ustore_listagg_0038_01(
     col_1 integer,
     col_2 bigint,
     col_3 float8,
     col_4 decimal(12,6),
     col_5 bool,
     col_6 char(30),
     col_7 varchar2(50),
     col_8 varchar(30),
     col_9 interval day to second,
     col_10 timestamp,
     col_11 date,
     col_12 smalldatetime,
     col_13 timestamp without time zone,
     col_14 blob,
     col_15 clob,
     col_16 int[]
) with (storage_type=ustore);

--step2:创建序列; expect:执行成功
drop sequence if exists seq_ustore_listagg_0038_01;
create sequence seq_ustore_listagg_0038_01 increment by 1 start with 10;

--step3:向表1中插入数据; expect:执行成功
insert into t_ustore_listagg_0038_01 values(1,seq_ustore_listagg_0038_01.nextval,   1+445.255,98*0.99,  true,lpad('abc','30','@'),lpad('abc','30','b'),rpad('abc','30','e'),(interval '4 5:12:10.222' day to second(3)),to_timestamp('2019-01-03 14:58:54.000000','yyyy-mm-dd hh24:mi:ss.ffffff'),
to_timestamp('2019-01-03','yyyy-mm-dd '),to_timestamp('2019-01-03','yyyy-mm-dd '),to_timestamp('2019-01-03','yyyy-mm-dd '),lpad('10','12','01010')::blob,rpad('abc','9','a@123&^%djgk'),'{32,535,5645645,6767,76,67,56,48,979,978,7}');
insert into t_ustore_listagg_0038_01 values(1,seq_ustore_listagg_0038_01.nextval,   1+445.255,98*0.99,  true,lpad('abc','30','@'),lpad('abc','30','b'),rpad('abc','30','e'),(interval '4 5:12:10.222' day to second(3)),to_timestamp('2019-01-03 14:58:54.000000','yyyy-mm-dd hh24:mi:ss.ffffff'),
to_timestamp('2019-01-03','yyyy-mm-dd '),to_timestamp('2019-01-03','yyyy-mm-dd '),to_timestamp('2019-01-03','yyyy-mm-dd '),lpad('10','12','01010')::blob,rpad('abc','9','a@123&^%djgk'),'{32,535,5645645,6767,76,67,56,48,979,978,7}');
insert into t_ustore_listagg_0038_01 values(1,seq_ustore_listagg_0038_01.nextval,   1+445.255,98*0.99,  true,lpad('abc','30','@'),lpad('abc','30','b'),rpad('abc','30','e'),(interval '4 5:12:10.222' day to second(3)),to_timestamp('2019-01-03 14:58:54.000000','yyyy-mm-dd hh24:mi:ss.ffffff'),
to_timestamp('2019-01-03','yyyy-mm-dd '),to_timestamp('2019-01-03','yyyy-mm-dd '),to_timestamp('2019-01-03','yyyy-mm-dd '),lpad('10','12','01010')::blob,rpad('abc','9','a@123&^%djgk'),'{32,535,5645645,6767,76,67,56,48,979,978,7}');
insert into t_ustore_listagg_0038_01 values(1,seq_ustore_listagg_0038_01.nextval,   1+445.255,98*0.99,  true,lpad('abc','30','@'),lpad('abc','30','b'),rpad('abc','30','e'),(interval '4 5:12:10.222' day to second(3)),to_timestamp('2019-01-03 14:58:54.000000','yyyy-mm-dd hh24:mi:ss.ffffff'),
 to_timestamp('2019-01-03','yyyy-mm-dd '),to_timestamp('2019-01-03','yyyy-mm-dd '),to_timestamp('2019-01-03','yyyy-mm-dd '),lpad('10','12','01010')::blob,rpad('abc','9','a@123&^%djgk'),'{32,535,5645645,6767,76,67,56,48,979,978,7}');
insert into t_ustore_listagg_0038_01 values(1,seq_ustore_listagg_0038_01.nextval,   1+445.255,98*0.99,  true,lpad('abc','30','@'),lpad('abc','30','b'),rpad('abc','30','e'),(interval '4 5:12:10.222' day to second(3)),to_timestamp('2019-01-03 14:58:54.000000','yyyy-mm-dd hh24:mi:ss.ffffff'),
to_timestamp('2019-01-03','yyyy-mm-dd '),to_timestamp('2019-01-03','yyyy-mm-dd '),to_timestamp('2019-01-03','yyyy-mm-dd '),lpad('10','12','01010')::blob,rpad('abc','9','a@123&^%djgk'),'{32,535,5645645,6767,76,67,56,48,979,978,7}');
insert into t_ustore_listagg_0038_01 values(1,seq_ustore_listagg_0038_01.nextval,   1+445.255,98*0.99,  true,lpad('abc','30','@'),lpad('abc','30','b'),rpad('abc','30','e')::blob,(interval '4 5:12:10.222' day to second(3)),to_timestamp('2019-01-03 14:58:54.000000','yyyy-mm-dd hh24:mi:ss.ffffff'),
to_timestamp('2019-01-03','yyyy-mm-dd '),to_timestamp('2019-01-03','yyyy-mm-dd '),to_timestamp('2019-01-03','yyyy-mm-dd '),lpad('10','12','01010')::blob,rpad('abc','9','a@123&^%djgk'),'{32,535,5645645,6767,76,67,56,48,979,978,7}');

--step4:创建测试表2并插入数据; expect:执行成功
drop table if exists t_ustore_listagg_0038_02;
create table t_ustore_listagg_0038_02 WITH (STORAGE_TYPE=USTORE) as select * from t_ustore_listagg_0038_01;
insert into t_ustore_listagg_0038_02(col_1,col_2,col_3,col_4,col_5,col_6,col_7) values(null,null,null,null,null,null,null);
insert into t_ustore_listagg_0038_02(col_1,col_2,col_3,col_4,col_5,col_6,col_7) values(null,null,null,null,null,null,null);
insert into t_ustore_listagg_0038_02(col_1,col_2,col_3,col_4,col_5,col_6,col_7) values(null,12,null,null,null,null,null);


--step5:join(子查询); expect:执行成功
select distinct a.col_1,b.c,c.f from t_ustore_listagg_0038_01 a
left join (
select col_1,col_2,substr(listagg(3,15) within group (order by col_1 desc nulls first,col_2 asc nulls first),1,4) c from t_ustore_listagg_0038_01 group by col_1,col_2
) b on a.col_1=b.col_1
left join(
select col_1,listagg('aaaaaaaaawrrrrrrrrrrggewr34rfdvf56yhgfghyurrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr') within group (order by col_8 asc nulls first) f from t_ustore_listagg_0038_02 group by col_1,col_2 order by 1,2
) c on a.col_1=c.col_1 order by 1,2;


select distinct a.col_1,b.c,c.f from t_ustore_listagg_0038_01 a
left join (
select col_1,col_2,listagg(3,15) within group (order by col_1 desc nulls first,col_2 asc nulls first,1,4) c from t_ustore_listagg_0038_01 group by col_1,col_2
) b on a.col_1=b.col_1
left join(
select col_1,listagg('aaaaaaaaawrrrrrrrrrrggewr34rfdvf56yhgfghyurrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr') within group (order by col_8 asc nulls first) f from t_ustore_listagg_0038_02 group by col_1,col_2 order by 1,2
) c on a.col_1=c.col_1 order by 1,2;

--step6:清理环境; expect:执行成功
drop sequence if exists seq_ustore_listagg_0038_01;
drop table if exists t_ustore_listagg_0038_01;
drop table if exists t_ustore_listagg_0038_02;
